package com.ks.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/getCookieServlet")
public class GetCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetCookieServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Cookie[] cookies = request.getCookies();
		PrintWriter out = response.getWriter();
		if (cookies == null) {
			out.print("NO COOKIE!");

		} else {
			for (Cookie cookie : cookies) {
				if ("username".equals(cookie.getName())) {
					out.println("UserName:" + cookie.getValue() + "<br />");
				}
				if ("password".equals(cookie.getName())) {
					out.println("Password:" + cookie.getValue() + "<br />");
				}
			}
		}
	}

}
